package controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cameraConfig.*;

public class CameraListController extends HttpServlet {


	private static final long serialVersionUID = 1L;
	
	public void process ( HttpServletRequest request, HttpServletResponse response ) {
		ServletContext context;
		RequestDispatcher dispatcher;
		String dispatcherTarget = "/cameraConfig.jsp";			
		context = getServletContext();
		
		KameraDAO camDAO = new KameraDAO();
		Vector<Standort> locations = new Vector<Standort>();
		locations = camDAO.getLocations();
		
		Vector<Kamera> cams = new Vector<Kamera>();
	
		//Map mit Locations und CamLists erstellen			
		Map<Standort, Vector<Kamera>> camsByLocation = new HashMap<Standort, Vector<Kamera>>();	
		
		for (Iterator<Standort> iterator = locations.iterator(); iterator.hasNext();) {
			Standort standort = (Standort) iterator.next();
			cams = camDAO.getCamObjectsIn(standort);
			camsByLocation.put(standort,cams);		
		}
		request.setAttribute("camsByLocation", camsByLocation);
//		Vector<Standort> s = (Vector<Standort>) camsByLocation.keySet();
//		Vector<Kamera> k = camsByLocation.get(s.get(0));
		
		dispatcher = context.getRequestDispatcher(dispatcherTarget);
				
		try {
			dispatcher.forward(request, response);
		} 
		catch (ServletException e) {
			e.printStackTrace();
			
		} 
		catch (IOException e) {
			e.printStackTrace();
		}
    }


	public void doGet ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
	
	public void doPost ( HttpServletRequest request, HttpServletResponse response ) {
		process(request, response);
	}
}
